import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{   
    int count=0; 
    GreenfootImage image1 = new GreenfootImage("crab.png");
    GreenfootImage image2 = new GreenfootImage("crab2.png");
public Crab(){
    setImage(image1);
}

public void act(){
       move(6);
       turntAtEdge();
       control();
       eat();
       animation();
}
    
public void animation(){
        if(getImage() == image1){
            setImage(image2);
    } else {
            setImage(image1);
    }
}
    
public void eat(){
        if(isTouching (Worm.class)){
            Actor actor=getOneIntersectingObject(Worm.class);   
            CrabWorld world=(CrabWorld)getWorld();
            world.removeObject(actor);
            world.drawNum(count);
            count++;
        }
        
        if(isTouching (Lobster.class)){
        CrabWorld world=(CrabWorld)getWorld();
        world.lose();
        }
}
    
public void turntAtEdge(){
        if (isAtEdge())
        {
        turn(5);
        }
}
    
public void control(){
       if(Greenfoot.isKeyDown("up")){
                turn(6);
       }
       if(Greenfoot.isKeyDown("right")){
                turn(-9);
       }
       if(Greenfoot.isKeyDown("down")){
                turn(6);
       }
       if(Greenfoot.isKeyDown("left")){
            turn(-9);
       }
}
}